//
//  UIViewController+NavagitionBar.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/19.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "UIViewController+NavagitionBar.h"

@implementation UIViewController (NavagitionBar)

- (void)setupLeftItemWithImageName:(NSString *)imageName action:(SEL)action {
    self.navigationItem.leftBarButtonItem = [self barButtonItemWithImageName:imageName action:action];
    
}

- (void)setupRightItemWithImageName:(NSString *)imageName action:(SEL)action {
    self.navigationItem.rightBarButtonItem = [self barButtonItemWithImageName:imageName action:action];
}

- (void)setupCenterItemWithText:(NSString *)text action:(SEL)action {
    self.navigationItem.titleView = [self searchItemWithText:text action:action];
}

- (void)setupRightItemWithText:(NSString *)text action:(SEL)action {
    self.navigationItem.rightBarButtonItem = [self barButtonItemWithText:text action:action];
}


- (void)setupBackItem {
    
    [self setupLeftItemWithImageName:@"个人中心-left" action:@selector(backButtonTaped:)];
    
}


- (UIBarButtonItem *)barButtonItemWithImageName:(NSString *)imageName action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    return barButtonItem;
    
    
}

- (UIBarButtonItem *)barButtonItemWithText:(NSString *)text action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setTitle:text forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.textColor = [UIColor whiteColor];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    return barButtonItem;
    
    
}

- (UIView*)searchItemWithText:(NSString *)text  action:(SEL)action {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidthScale * 230, kScreenHeightScale * 30)];
    UITextField *textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(0, 0, kScreenWidthScale * 230, kScreenHeightScale * 30);
    textField.userInteractionEnabled = NO;
    UIView *imageview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 7, 15, 15)];
    [image setImage:[UIImage imageNamed:@"放大镜"]];
    [imageview addSubview:image];
    textField.leftView = imageview;
    [textField setLeftViewMode:UITextFieldViewModeAlways];
    [textField setBackground:[UIImage imageNamed:@"搜索框"]];
    textField.font = [UIFont systemFontOfSize:12];
    textField.placeholder = text;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = textField.frame;
    [view addSubview:button];
    [view addSubview:textField];
//    textField.can;
    
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    
//    [textField setValue:@(NO) forKey:@"editing"];
    return view;
}


- (UITextField *)memberTextFieldText:(NSString *)text image:(NSString *)image label:(NSString *)label {
    UITextField *textField = [[UITextField alloc]init];
    textField.backgroundColor = [UIColor whiteColor];
    textField.borderStyle = UITextBorderStyleNone;
    textField.placeholder = text;
    textField.font = [UIFont systemFontOfSize:13];
    UIView *view = [[UIView alloc]init];
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = RGBCOLOR(140.0f, 140.0f, 140.0f, 1);
    if (image != nil) {
        view.frame = CGRectMake(0, 0, 60, 50);
        line.frame = CGRectMake(50, 17, 1, 15);

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20,13, 25, 25)];
        imageView.image = [UIImage imageNamed:image];
        [view addSubview:imageView];
        textField.leftView = view;
       

    }
    if (label != nil) {
        view.frame = CGRectMake(0, 0, 100, 50);
        line.frame = CGRectMake(90, 17, 1, 15);

        UILabel *labeltext = [[UILabel alloc]initWithFrame:CGRectMake(20,13, 70, 25)];
        labeltext.font = [UIFont systemFontOfSize:13];
        labeltext.text = label;
        [view addSubview:labeltext];
        textField.leftView = view;

    }
   
    
    [textField setLeftViewMode:UITextFieldViewModeAlways];
    
 
    
    [view addSubview:line];
    
    return textField;
    
    
}




- (void)backButtonTaped:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)addAlertControllerWithTitle:(NSString *)title message:(NSString *)message actionNum:(NSInteger)actionNum style:(UIAlertControllerStyle)style handler:(void (^)(UIAlertAction * action)) handler{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    if (actionNum == 1) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:handler];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else if(actionNum == 2){
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDefault handler:handler];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
